package cn.sumpu.app.guoguojie.biz.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sumpu.app.guoguojie.biz.ProductCategoryBiz;
import cn.sumpu.app.guoguojie.dao.ProductCategoryDao;
import cn.sumpu.app.guoguojie.domain.ProductCategory;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class ProductCategoryBizImpl implements ProductCategoryBiz {
	private ProductCategoryDao productCategoryDao;

	public void setProductCategoryDao(ProductCategoryDao productCategoryDao) {
		this.productCategoryDao = productCategoryDao;
	}

	public List<ProductCategory> buildProductCategory()
			throws AppBusinessException {
		return getProductCategory("0");
	}

	public List<ProductCategory> buildProductCategory(String parentId)
			throws AppBusinessException {
		return getProductCategory(parentId);
	}

	public String getProductCategoryPath(String id, String separator)
			throws AppBusinessException {
		StringBuilder path = new StringBuilder("");
		return getPath(id, separator, path);
	}

	public String getChildrenCategoryId(String parentId)
			throws AppBusinessException {
		List<String> categoryBuilder = new ArrayList<String>();
		return getCategory(parentId, categoryBuilder);
	}

	private List<ProductCategory> getProductCategory(String parentId)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("parentIdentifier", parentId);
		List<ProductCategory> mcList = productCategoryDao
				.getProductCategoryList(map);
		for (ProductCategory mc : mcList) {
			mc.setChildren(getProductCategory(mc.getIdentifier()));
		}
		return mcList;
	}
	public List<ProductCategory> getproductCategoryListById(String parentId)throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("parentIdentifier", parentId);
		List<ProductCategory> mcList = productCategoryDao
		.getProductCategoryList(map);
		return mcList;
	}

	private String getCategory(String id, List<String> categoryBuilder)
			throws AppBusinessException {
		List<ProductCategory> cList = buildProductCategory(id);
		for (ProductCategory tmp : cList) {
			categoryBuilder.add("'" + tmp.getIdentifier() + "'");
			List<ProductCategory> children = tmp.getChildren();
			if (null != children && children.size() > 0) {
				getCategory(tmp.getIdentifier(), categoryBuilder);
			}
		}
		StringBuilder s = new StringBuilder();
		for (String tmp : categoryBuilder) {
			s.append(tmp + ",");
		}
		s.append("'" + id + "'");
		return s.toString();

	}

	private String getPath(String id, String separator, StringBuilder path)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("identifier", id);
		ProductCategory mc = productCategoryDao.getProductCategoryById(map);
		String parentId = mc.getParentIdentifier();
		if ("0".equals(parentId)) {
			path.insert(0, mc.getName());
		} else {
			path.insert(0, separator + mc.getName());
			getPath(parentId, separator, path);
		}
		return path.toString();
	}
}
